package com.banmao.desktoptools.testmybatis.controller;

import com.banmao.desktoptools.FXMain;
//import com.banmao.desktoptools.copyfilesScene.service.CopyfilesService;
import com.banmao.desktoptools.testmybatis.dao.TestMybatisMapper;
import com.banmao.desktoptools.testmybatis.vo.UserPojo;
import com.banmao.desktoptools.testmybatis.dao.TestMybatisDao2;
import com.banmao.desktoptools.testmybatis.vo.UserPojo2;
import com.banmao.desktoptools.util.SpringContextUtil;
//import com.baomidou.mybatisplus.core.conditions.Wrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
import javafx.stage.DirectoryChooser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.ResourceBundle;

@Slf4j
@FXMLController
public class TestMybatisController implements Initializable {

    @FXML
    private Text helloText;

    @FXML
    private Button helloButton;

    private Boolean flag = true;

    private TestMybatisMapper testMybatisMapper;

    private TestMybatisDao2 testMybatisDao2;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        try {
            testMybatisMapper = SpringContextUtil.getBean(TestMybatisMapper.class);
        } catch (BeansException e) {
            log.error("未获取到Bean testMybatisDao");
        }
        try {
            testMybatisDao2 = SpringContextUtil.getBean(TestMybatisDao2.class);
        } catch (BeansException e) {
            log.error("未获取到Bean testMybatisDao2");
        }
    }

    public void changeTextByMP(ActionEvent actionEvent) {
        String user_name = "null";
        if(testMybatisDao2 != null) {
            log.info("copyfilesDao");
            QueryWrapper<UserPojo2> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.select("user_name");
            List<UserPojo2> data = testMybatisDao2.selectList(objectQueryWrapper);
            //List<UserPojo> data = copyfilesDao.getAllUser();
            if(data.size() > 0) {
                UserPojo2 userPojo = data.get(new Random().nextInt(data.size() - 1));
                user_name = (String) userPojo.getUserName();
            }
        }

        String path = "";

        if(flag) {
            helloText.setText("世界 MP " + user_name + "\n" + path);
        } else {
            helloText.setText("你好 MP " + user_name + "\n" + path);
        }
        flag = !flag;
    }

    public void changeTextByMybatis(ActionEvent actionEvent) {
        String user_name = "null";
        if(testMybatisMapper != null) {
            log.info("copyfilesDao");
            List<UserPojo> data = testMybatisMapper.getAllUser();
            if(data.size() > 0) {
                UserPojo userPojo = data.get(0);
                user_name = (String) userPojo.getUserName();
            }
        }

        String path = "";
        if(flag) {
            helloText.setText("世界 Mybatis " + user_name + "\n" + path);
        } else {
            helloText.setText("你好 Mybatis " + user_name + "\n" + path);
        }
        flag = !flag;
    }

}
